package com.intpro.demo.Service;

import com.intpro.demo.DAO.Mysql.Model.Expert;
import com.intpro.demo.DAO.Mysql.Model.Paper;
import com.intpro.demo.DAO.Mysql.Repository.PaperRepository;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaperService
{

	@Autowired
	private PaperRepository paperRepository;

	public Paper getPaper(int pid)
	{
		Paper p = paperRepository.getPaper(pid);
		var l = paperRepository.getAllAuthor(pid);
		p.experts = new LinkedList<>();
		for (var i : l)
		{
			Expert e = paperRepository.getExpert(i.AuthorID);
			e.AuthorType = i.AuthorType;
			p.experts.add(e);
		}
		return p;
	}

	public Expert getExpert(int id)
	{
		return paperRepository.getExpert(id);
	}

	public List<Paper> getPaperByExpert(int eid)
	{
		var l = paperRepository.getAllPaper(eid);
		List<Paper> p = new LinkedList<>();
		for (var i : l)
		{
			p.add(paperRepository.getPaper(i.PaperID));
		}
		return p;
	}

	public List<Expert> getRelatedExpert(int eid)
	{
		var l = paperRepository.getAllPaper(eid);
		HashSet<Integer> s = new HashSet<>();
		for (var i : l)
		{
			var l1 = paperRepository.getAllAuthor(i.PaperID);
			for (var j : l1)
			{
				s.add(j.AuthorID);
			}
		}
		s.remove(eid);
		List<Expert> res = new LinkedList<>();
		for (var i : s)
		{
			res.add(paperRepository.getExpert(i));
		}
		return res;
	}

}
